const express = require("express");
const router = express.Router();
var dayjs = require("dayjs");
// 导入db
const { getAll, getOne, exec } = require("../db");

// 获取所有notice，Get /gg_notice
// router.get("/gg_notice", async (req, res) => {
//   let sql = `select * from gg_notice order by notice_time desc`;
//   try {
//     // 成功
//     const data = await getAll(sql);
//     res.send({
//       code: 0,
//       message: "成功获取公告",
//       result: data,
//     });
//   } catch (err) {
//     res.send({
//       code: 100101,
//       message: "未能获取公告",
//       result: debug ? err : "",
//     });
//   }
// });
/**
 * 获取所有文章 以及文章总数
 * GET /gg_notice 
 * GET /gg_notice?page=1&size=2 分页查询
 */
router.get('/gg_notice', async (req, res) => {
  // 注意：无论获取什么类型的数据，都需要传这三个参数
  // 获取响应数据时再调用需要的数据
  const { page, size } = req.query;
  // 计算偏移量
  let sql = `select count(*) as total from gg_notice`;
  // console.log(sql);
  const num = await getOne(sql);
  console.log(num);
  //分页公式: 偏移量 = (当前页码 - 1) * 每页显示数
  if (page) {
    // 偏移量
    // console.log(page, size);
    let offset = (page - 1) * size;
    sql = `select * from gg_notice order by notice_time desc limit ${offset},${size} `;
    /*     sql = `select articles.*,tags.tags from articles,tags where  delete_time is null and articles.tags_id=tags.id ORDER BY create_time DESC limit ${offset},${size} `; */
    const data = await getAll(sql);
    res.send({
      code: 0,
      message: '获取分页数据成功',
      result: {
        data,
        total: num.total,
      },
    });
  } else {
    // 直接获取所有文章
    let sql = `select * from gg_notice order by notice_time desc`;
    const data = await getAll(sql);
    res.send({
      code: 0,
      message: '获取所有成功',
      result: {
        data,
        total: num.total,
      },
    });
  }
})
//获取最新的两个公告 GET/gg_notice
// router.get("/gg_notice", async (req, res) => {
//   // 执行sql语句
//   let sql = `select * from gg_notice order by notice_time desc limit 0,2`;
//   const data = await getAll(sql);
//   res.send({
//     code: 0,
//     message: "获取最新两个公告成功",
//     result: {
//       data: data,
//     },
//   });
// });

// 根据id获取notice  Get/gg_notice/:id
router.get("/gg_notice/:id", async (req, res) => {
  //获取id
  let { id } = req.params;
  //执行sql语句
  let sql = `select * from gg_notice where notice_id=${id}`;
  try {
    var data = await getOne(sql);
  } catch (err) {
    res.send({
      code: 100103,
      message: "未能查询单个公告",
      result: debug ? err : "",
    });
  }
  if (data) {
    res.send({
      code: 0,
      message: "成功获取公告",
      result: data,
    });
  } else {
    res.status(404).send({
      code: 100102,
      message: "未能查询到id对应公告",
      result: "",
    });
  }
});

// 添加事件 POST/gg_notice
router.post("/gg_notice", async (req, res) => {
  // 解析user_id和notice_content
  const { user_id, notice_title, notice_content } = req.body;
  // 执行添加语句
  let sql = `insert into gg_notice (user_id,notice_title,notice_content) values ('${user_id}','${notice_title}','${notice_content}')`;
  try {
    const data = await exec(sql);
    res.send({
      code: 0,
      msg: "添加公告成功",
      result: {
        notice_id: data.insertId,
        user_id,
        notice_title,
        notice_content,
      },
    });
  } catch (error) {
    res.send({
      code: 100104,
      msg: "无法添加公告",
      result: debug ? error : "",
    });
  }
});

//修改事件 PUT/gg_notice/:id
router.put("/gg_notice/:id", async (req, res) => {
  // 解析id
  let { id } = req.params;
  let { user_id, notice_content, notice_time } = req.body;
  // 执行更新语句
  let sql = `update gg_notice set user_id='${user_id}',notice_content='${notice_content}',notice_time='${notice_time}' where notice_id=${id}`;
  await exec(sql);
  try {
    res.send({
      code: 0,
      message: "修改公告成功",
      result: {
        notice_id: id,
        user_id,
        notice_content,
        notice_time,
      },
    });
  } catch (error) {
    res.send({
      code: 100105,
      msg: "无法修改公告",
      result: debug ? error : "",
    });
  }
});

// 删除事件 Delete/gg_notice/:id
router.delete("/gg_notice/:id", async (req, res) => {
  // 解析id
  let { id } = req.params;
  //执行delete语句
  let sql = `delete from gg_notice where notice_id=${id}`;
  await exec(sql);
  try {
    res.send({
      code: 0,
      message: "成功删除公告",
      result: "",
    });
  } catch (error) {
    res.send({
      code: 100106,
      msg: "无法删除公告",
      result: debug ? error : "",
    });
  }
});
module.exports = router;
